import { reactive, onMounted, ref, nextTick } from 'vue'
import useInstance from '@/hooks/useInstance';
import { roleSaveApi, roleUpdateApi, roleRemoveApi } from '@/api/role/role'
import { RolePageParams, RoleTable } from '@/api/role/roleModel'
import { EditType } from '@/type/BaseEnum'
export default function RoleEdit(searchParams, getRoleList, roleTableDate) {
  const { global, proxy } = useInstance()
  const RefRoleAddEdit = ref<{ show: (type: string, row?: any) => void }>()
  const addBtn = () => {
    RefRoleAddEdit?.value?.show(EditType.ADD)
  }
  const editBtn = (row) => {
    RefRoleAddEdit?.value?.show(EditType.EDIT, row)
  }
  const save = async (row) => {
    let res
    if (row?.id) {
      res = await roleUpdateApi(row)
    } else {
      res = await roleSaveApi(row)
    }
    if (res.code === 200 && row?.id) {
      global.$message({ message: '编辑成功', type: 'success' })
      searchParams.page = 1
      getRoleList()
    } else {
      global.$message({ message: '新增成功', type: 'success' })
      searchParams.page = 1
      getRoleList()
    }
  }
  const deleteBtns = async (id: number | string) => {
    const confirm = await global.$myconfirm('确定删除该部门嘛?')
    if (confirm) {
      const res = await roleRemoveApi({ id })
      if (res.code === 200) {
        global.$message({ message: '删除成功', type: 'success' })
        if (roleTableDate.list.length === 1 && searchParams.page > 1) {
          searchParams.page -= 1
          getRoleList()
        } else {
          getRoleList()
        }
      }
    } else {
      global.$message({ message: "已取消删除", type: 'warning' })
    }
  }
  return {
    RefRoleAddEdit, addBtn, save, editBtn, deleteBtns
  }
}